package edu.scau.cxq.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.cxq.gp.service.IRecordInService;
import edu.scau.cxq.gp.entity.Goods;
import edu.scau.cxq.gp.entity.Inventory;
import edu.scau.cxq.gp.entity.RecordIn;
import edu.scau.cxq.gp.entity.Warehouse;
import edu.scau.cxq.gp.mapper.GoodsMapper;
import edu.scau.cxq.gp.mapper.InventoryMapper;
import edu.scau.cxq.gp.mapper.RecordInMapper;
import edu.scau.cxq.gp.mapper.WarehouseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.scau.cxq.gp.entity.vo.RecordInVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 存储入库信息 服务实现类
 * </p>
 *
 * @author CXQ
 * @since 2022-01-30
 */
@Service
public class RecordInServiceImpl extends ServiceImpl<RecordInMapper, RecordIn> implements IRecordInService {

    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private RecordInMapper recordInMapper;

    /**
     * 添加入库信息之前进行的一些操作
     * 比如： 更新仓库空闲货架个数 添加仓库存货记录（inventory）
     *
     * @param recordIn
     * @return
     */
    @Override
    public boolean beforeInsertRecordIn(RecordIn recordIn) {

        // 查询一个货架可以放多少个该货物
        Goods goods = goodsMapper.selectById(recordIn.getGoodsId());
        Integer goodsCosts = goods.getGoodsCosts();

        // 有意设计的冗余字段
        int amount = (int) Math.ceil((double) recordIn.getInCount() / goodsCosts);
        recordIn.setInAmount(amount);

        // 减少仓库空闲货架数
        Warehouse warehouse = warehouseMapper.selectById(recordIn.getWareId());

        // 超出库存则让它结束
        if (warehouse.getWareFree() < amount) {
            return false;
        }

        // 添加库存记录
        // 保存库存信息
        Inventory inventory = new Inventory();
        BeanUtils.copyProperties(recordIn, inventory);
        inventory.setInGoodsAmount(recordIn.getInAmount())
                .setInGoodsCost(goodsCosts).
                setInGoodsCount(recordIn.getInCount())
                .setInGoodsPrice(recordIn.getInPrice());
        inventoryMapper.insert(inventory);

        warehouse.setWareFree(warehouse.getWareFree() - amount);
        warehouseMapper.updateById(warehouse);

        return true;
    }

    /**
     * 条件查找出库记录
     *
     * @param goodsId
     * @param supId
     * @param wareId
     * @return
     */
    @Override
    public PageInfo<RecordInVO> getRecordInVOList(Integer currentPage, Integer size, Long goodsId, Long supId, Long wareId) {
        PageHelper.startPage(currentPage, size);
        List<RecordInVO> recordInVOS = recordInMapper.selectRecordInVOList(goodsId, supId, wareId);
        PageInfo<RecordInVO> recordInVOPageInfo = new PageInfo<>(recordInVOS);
        return recordInVOPageInfo;
    }

    /**
     * 撤销入库记录
     *
     * @param inId
     * @return
     */
    @Override
    @Transactional
    public boolean rollback(Long inId) {

        // 找出入库记录
        RecordIn recordIn = recordInMapper.selectById(inId);

        QueryWrapper<Inventory> inventoryQueryWrapper = new QueryWrapper<>();
        inventoryQueryWrapper.eq("ware_id", recordIn.getWareId());
        inventoryQueryWrapper.eq("goods_id", recordIn.getGoodsId());
        inventoryQueryWrapper.eq("in_goods_price", recordIn.getInPrice());
        inventoryQueryWrapper.eq("in_goods_count", recordIn.getInCount());

        Inventory inventory = inventoryMapper.selectOne(inventoryQueryWrapper);

        // 除非一模一样 否则不能撤回
        if (inventory == null) {
            return false;
        }

        // 删除库存
        inventoryMapper.deleteById(inventory.getInvId());

        Integer inGoodsAmount = inventory.getInGoodsAmount(); // 这批货物占用的货架数（如果数据被动用了 就不能撤回了）

        Long wareId = inventory.getWareId();


        // 更新仓库空闲货架数
        Warehouse warehouse = warehouseMapper.selectById(wareId);
        warehouse.setWareFree(warehouse.getWareFree() + inGoodsAmount);

        warehouseMapper.updateById(warehouse);


        // 删除入库记录
        recordInMapper.deleteById(inId);

        return true;
    }
}
